#!/bin/bash

set -e

# Including common functions
. "${LB_BASE:-/usr/share/live/build}"/scripts/build.sh

# Setting static variables
DESCRIPTION="$(Echo 'installing syslinux UEFI bootloader')"
HELP=""
USAGE="${PROGRAM}"

# Reading configuration files
Read_conffiles config/all config/bootstrap config/common config/binary
Set_defaults

# Safeguards
[ "${LB_BOOTLOADER}"   = "syslinux" ] || exit 0
[ "${LB_ARCHITECTURE}" = "i386"     ] || exit 0

# Seems like we'll have work to do
Echo_message "installing syslinux UEFI bootloader"

# Setting boot method specific variables
case "${LB_BINARY_IMAGES}" in
	iso|iso-hybrid)
		SYSLINUX_PATH="binary/isolinux"
		;;
	usb-hdd)
		SYSLINUX_PATH="binary/syslinux"
		;;
esac

# Main
mkdir -p binary/EFI/BOOT
cp chroot/usr/lib/SYSLINUX.EFI/efi64/syslinux.efi binary/EFI/BOOT/bootx64.efi
cp chroot/usr/share/tails/bootx64.png binary/EFI/BOOT/
cp "$SYSLINUX_PATH"/* binary/EFI/BOOT/
cp -f chroot/usr/lib/syslinux/modules/efi64/* binary/EFI/BOOT/
sed -r -i -e 's,^(menu background splash\.png)$,\#\1,' binary/EFI/BOOT/stdmenu.cfg
